package com.jy.adscm.hospital.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.hospital.bean.JyHospitalPersonnelTeam;
import com.jy.adscm.hospital.bean.JyHospitalRemarks;
import com.jy.adscm.hospital.mapper.JyHospitalPersonnelTeamMapper;
import com.jy.adscm.hospital.service.JyHospitalPersonnelTeamService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 人员团队信息 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-16
 */
@Service
public class JyHospitalPersonnelTeamServiceImpl extends ServiceImpl<JyHospitalPersonnelTeamMapper, JyHospitalPersonnelTeam> implements JyHospitalPersonnelTeamService {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;

    @Autowired
    DealerMapper dealerMapper;

    @Override
    public List<JyHospitalPersonnelTeam> selectList(Wrapper<JyHospitalPersonnelTeam> wrapper) {

        logger.info("查询医院人员团队信息...");

        List<JyHospitalPersonnelTeam> jyHospitalPersonnelTeams = super.selectList(wrapper);

        if (jyHospitalPersonnelTeams != null && jyHospitalPersonnelTeams.size()>0){
            for (JyHospitalPersonnelTeam jyHospitalPersonnelTeam : jyHospitalPersonnelTeams) {
                Integer inputUserId = jyHospitalPersonnelTeam.getInputUserId();
                if (inputUserId != null){
                    JyUser user = jyUserMapper.selectById(inputUserId);
                    if (user != null){
                        jyHospitalPersonnelTeam.setInputUserName(user.getName());
                    }
                }

                Integer inputUnit = jyHospitalPersonnelTeam.getInputUnit();
                if (inputUnit == 0){
                    jyHospitalPersonnelTeam.setInputUnitDesc("生产厂商");
                }else {
                    Dealer dealer = dealerMapper.selectById(inputUnit);
                    if (dealer != null){
                        jyHospitalPersonnelTeam.setInputUnitDesc(dealer.getName());
                    }
                }

                Long sectionOffice = jyHospitalPersonnelTeam.getSectionOffice();

                if (sectionOffice != null){

                    List<JyCode> hospital_office = jyCodeMapper.getJyCodeByCodeType("hospital_office");
                    if (hospital_office != null && hospital_office.size()>0){
                        for (JyCode jyCode : hospital_office) {
                            long code = Long.parseLong(jyCode.getCode());

                            if (code == sectionOffice){
                                jyHospitalPersonnelTeam.setSectionOfficeName(jyCode.getName());
                            }
                        }
                    }

//                    JyCode jyCode = jyCodeMapper.selectById(sectionOffice);
//                    if (jyCode != null){
//                        jyHospitalPersonnelTeam.setSectionOfficeName(jyCode.getName());
//                    }
                }

            }
        }

        return jyHospitalPersonnelTeams;
    }
}
